<?php

require_once 'common/common.php';
require_once 'common/conn.php';

$data = array();

$usercode = getParameter("usercode");

if ($usercode != "") {
    $db = new DBC();
    $uid = getUseridByUsercode($usercode);
    $table_record = "t_game_record_" . $uid;
    $table = "t_game_detail_" . $uid;

    $sql = "SELECT result FROM " . $table_record;
    $array = $db->fetchData($sql);

    $finish = count($array);
    $win = 0;
    if (checkArrayNotEmpty($array)) {
        for ($i = 0; $i < count($array); $i++) {
            if ($array[$i]['result'] == "1") {
                $win++;
            }
        }
    }

    $sql = "SELECT SUM(aProperty) * 15 + SUM(bProperty) * 30 + SUM(cProperty) * 10 + SUM(dProperty) * 40 + SUM(eProperty) * 2 + SUM(fProperty) * 2 AS total FROM " . $table;
    $gold_array = $db->fetchData($sql);
    if (checkArrayNotEmpty($gold_array)) {
        $gold = $gold_array[0]['total'];
    } else {
        $gold = 0;
    }

    $sql = "SELECT SUM(aProperty) AS a,SUM(bProperty) AS b,SUM(cProperty) AS c,SUM(dProperty) AS d,SUM(eProperty) AS e,SUM(fProperty) AS f FROM " . $table;
    $property_data = $db->fetchData($sql);

    $sql = "SELECT praise FROM t_device WHERE devicecode = '" . $usercode . "'";
    $user = $db->fetchData($sql);

    $sql = "SELECT DISTINCT i.*,r.usercode,r.paid,r.times FROM t_achieve_info i LEFT JOIN t_achieve_record r ON i.code = r.achievecode AND usercode = '" . $usercode . "'";

    $data = $db->fetchData($sql);
    for ($i = 0; $i < count($data); $i++) {
        $data[$i]['finish'] = "0";
        $data[$i]['times'] = 0;
        if ($data[$i]['usercode'] == $usercode) {
            $data[$i]['finish'] = "1";
        }

        if ($data[$i]['usercode'] == $usercode) {
            $data[$i]['finish'] = "1";
        }
        if ($data[$i]['paid'] != "1") {
            $data[$i]['paid'] = "0";
        }
        if (isNull($data[$i]['usercode'])) {
            $data[$i]['usercode'] = $usercode;
        }
        if ($data[$i]['code'] == 'achieve10' || $data[$i]['code'] == 'achieve20' || $data[$i]['code'] == 'achieve30' || $data[$i]['code'] == 'achieve40') {
            $data[$i]['times'] = $finish;
        }
        if ($data[$i]['code'] == 'achieve50' || $data[$i]['code'] == 'achieve60' || $data[$i]['code'] == 'achieve70' || $data[$i]['code'] == 'achieve80') {
            $data[$i]['times'] = $win;
        }
        if ($data[$i]['code'] == 'achieve360' || $data[$i]['code'] == 'achieve370' || $data[$i]['code'] == 'achieve380') {
            $data[$i]['times'] = $gold;
        }

        /*
         *  achieve390	使用10次以上增加时间道具c
          achieve400	使用50次以上增加时间道具    c
          achieve410	使用10次以上增加生命道具 b
          achieve420	使用50次以上增加生命道具b
          achieve430	使用10次以上炸弹道具d
          achieve440	使用50次以上炸弹道具d
          achieve450	使用10次以上橡皮擦道具a
          achieve460	使用50次以上橡皮擦道具a
         * // a擦子、b加血、c冻结、d炸弹、e赞赏、f嘲讽
         *
         * achieve310	使用50次嘲讽道具f
          achieve320	使用50次赞美道具e
          achieve330	被别人赞美超过100次
         */
        if (checkArrayNotEmpty($property_data)) {
            if ($data[$i]['code'] == 'achieve390' || $data[$i]['code'] == 'achieve400') {
                $data[$i]['times'] = $property_data[0]['c'];
            }
            if ($data[$i]['code'] == 'achieve410' || $data[$i]['code'] == 'achieve420') {
                $data[$i]['times'] = $property_data[0]['b'];
            }
            if ($data[$i]['code'] == 'achieve430' || $data[$i]['code'] == 'achieve440') {
                $data[$i]['times'] = $property_data[0]['d'];
            }
            if ($data[$i]['code'] == 'achieve450' || $data[$i]['code'] == 'achieve460') {
                $data[$i]['times'] = $property_data[0]['a'];
            }
            if ($data[$i]['code'] == 'achieve310') {
                $data[$i]['times'] = $property_data[0]['f'];
            }
            if ($data[$i]['code'] == 'achieve320') {
                $data[$i]['times'] = $property_data[0]['e'];
            }
        }
        if (checkArrayNotEmpty($user)) {
            if ($data[$i]['code'] == 'achieve330') {
                $data[$i]['times'] = $user[0]['praise'];
            }
        }
        if (isNull($data[$i]['times'])) {
            $data[$i]['times'] = 0;
        }
    }
}
echo json_encode($data);

function getUseridByUsercode($usercode) {
    $db = new DBC();
    $sql = "SELECT deviceid FROM t_device WHERE devicecode = '" . $usercode . "'";
    $data = $db->fetchData($sql);
    if (checkArrayNotEmpty($data)) {
        return $data[0]['deviceid'];
    }
    return 0;
}

/*
 * achieve10	完成1局对战
  achieve20	完成50局对战
  achieve30	完成200局对战
  achieve40	完成1000局对战

  胜局
  achieve50	获得1次对战胜利
  achieve60	获得50次对战胜利
  achieve70	获得200次对战胜利
  achieve80	获得1000次对战胜利

  achieve270	正确500道不同的题目
  achieve280	正确1000道不同的题目
  achieve290	错误500道不同的题目
  achieve300	错误1000道不同的题目
 *



 */
?>